package utilities.event;

import hibernatesrc.billStream.Bill;
import hibernatesrc.billStream.ConcreteBill;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Session;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;

import utilities.Updatable;
import utilities.utilities_components.SumUp;

public class NDGLListener implements EventListener{
	private SumUp controller;
	private Updatable updateItem;
	
	public NDGLListener( SumUp controller, Updatable updateItem ){
		this.controller = controller;
		this.updateItem = updateItem;
	}
	
	public void onEvent( Event event ){
		Calendar calendar = controller.getDate();
		
		updateItem.update( getData( calendar.get( Calendar.YEAR ), calendar.get( Calendar.MONTH ) ) );
	}
	
	//List中的数据为当月每天的结账起数,与结账总金额
	private List getData( int year, int month, Session session ){
		List ret=new ArrayList();
		
		Calendar from=Calendar.getInstance();
		
		from.set(Calendar.YEAR,year);
		from.set(Calendar.MONTH,month);
		from.set(Calendar.DATE,1);
		Date fromTime=from.getTime();
		
		from.set(Calendar.MONTH,month+1);
		Date toTime=from.getTime();
		
		List bills=getBillByTime(fromTime,toTime,Bill.getClass(),session);
		Iterator itr=bills.iterator();
		ConcreteBill temp=null;
		
		ArrayList newCol=null;
		for(int i=1;i < 31;i++){
			newCol=new ArrayList();
			ret.add(newCol);
			newCol.add(new Integer(0));
			newCol.add(new Double(0.0));
		}
		
		while(itr.hasNext()){
			temp=(ConcreteBill)itr.next();
			Date time=temp.getTimePay();
			int date=time.getDate();
			if(date >= 1 && date <=31){
				ret.get(date-1).add(0,new Integer(1+(int)(((ArrayList)ret.get(date)).get(0))));
				ret.get(date-1).add(1,new Double(temp.getPrice()+(double)(((ArrayList)ret.get(date)).get(1))));
			}
		}
		
		for(int i=30;i >= 0;i--)
			if(((Integer)((ArrayList)ret.get(i)).get(0)) == 0)
				((ArrayList)ret.get(i)).remove(i);
		
		return ret;
	}
}

}
